/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.io;

import org.dopdf.utils.Constants;

/**
 * The class that is responsible to resolve the type of the content that is served.  This is determined from the resourcePath
 * of the content.
 * <ul><li>If the content ends as png content type is image/png</li>
 *     <li>If the content ends in js or css the content type is plain/text </li></ul>
 *
 * @author Deep Shah
 */
public class ContentTypeResolver {

    /** The resource resourcePath whose content type is to be determined. */
    private String resourcePath;

    /**
     * The constructor that takes in the resourcePath as argument.
     * @param resourcePath the resourcePath reference for which we have to find the content type.
     */
    public ContentTypeResolver(final String resourcePath) {
        this.resourcePath = resourcePath;
    }

    /**
     * The method that will determin what is the content type of the resource.
     * @return the string representing the content type of the resource.
     */
    public String getType() {
        if(resourcePath.endsWith(Constants.Resource.JPEG_FORMAT)) return Constants.Resource.JPEG_CONTENT_TYPE;
        if(resourcePath.endsWith(Constants.Resource.PNG_FORMAT)) return Constants.Resource.PNG_CONTENT_TYPE;
        if(resourcePath.endsWith(Constants.Resource.JAVASCRIPT_FORMAT)
                || resourcePath.endsWith(Constants.Resource.CSS_FORMAT)) return Constants.Resource.TEXT_CONTENT_TYPE;
        return Constants.Resource.TEXT_CONTENT_TYPE;
    }
}
